/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Login;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import MinaClient.*;
import airtrafficcontrol.Clearence_Delivery.Clearence_Delivery;


/**
 *
 * @author Administrator
 */
public class LoginGUI extends javax.swing.JFrame {

    /**
     * Creates new form LoginGUI
     */
    public LoginGUI() {
        initComponents();
        this.setLocationRelativeTo(null); 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_Login_MainTitle = new javax.swing.JLabel();
        label_Login_SubTitle = new javax.swing.JLabel();
        label_Login_Label = new javax.swing.JLabel();
        label_Login_Username = new javax.swing.JLabel();
        label_Login_Password = new javax.swing.JLabel();
        jPasswordField_Login_Password = new javax.swing.JPasswordField();
        jTextField_Login_Username = new javax.swing.JTextField();
        jButton_Login_Login = new javax.swing.JButton();
        jButton_Login_ForgotPassword = new javax.swing.JButton();
        jTextField_Login_InputVerifyCode = new javax.swing.JTextField();
        jLabel_Login_VerifyCode = new javax.swing.JLabel();
        jLabel_Login_Message = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setFont(new java.awt.Font("微软雅黑", 1, 12)); // NOI18N

        label_Login_MainTitle.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        label_Login_MainTitle.setText("CSCI321");

        label_Login_SubTitle.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        label_Login_SubTitle.setText("Air Traffic Controller");

        label_Login_Label.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        label_Login_Label.setText("LOGIN");

        label_Login_Username.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        label_Login_Username.setText("Username:");
        label_Login_Username.setMaximumSize(new java.awt.Dimension(65, 20));
        label_Login_Username.setMinimumSize(new java.awt.Dimension(65, 20));
        label_Login_Username.setName(""); // NOI18N

        label_Login_Password.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        label_Login_Password.setText("Password:");
        label_Login_Password.setMaximumSize(new java.awt.Dimension(65, 20));
        label_Login_Password.setMinimumSize(new java.awt.Dimension(65, 20));

        jPasswordField_Login_Password.setMaximumSize(new java.awt.Dimension(160, 20));
        jPasswordField_Login_Password.setMinimumSize(new java.awt.Dimension(160, 20));

        jTextField_Login_Username.setMaximumSize(new java.awt.Dimension(160, 20));
        jTextField_Login_Username.setMinimumSize(new java.awt.Dimension(160, 20));

        jButton_Login_Login.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jButton_Login_Login.setText("Login");
        jButton_Login_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Login_LoginActionPerformed(evt);
            }
        });

        jButton_Login_ForgotPassword.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jButton_Login_ForgotPassword.setText("Forgot Password");

        jLabel_Login_VerifyCode.setText("Click Here");
        jLabel_Login_VerifyCode.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel_Login_VerifyCodeMouseClicked(evt);
            }
        });

        jLabel_Login_Message.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel_Login_Message.setForeground(new java.awt.Color(255, 0, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label_Login_MainTitle)
                    .addComponent(label_Login_SubTitle)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(label_Login_Password, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label_Login_Username, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
                            .addComponent(label_Login_Label))
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField_Login_InputVerifyCode, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(jLabel_Login_VerifyCode, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel_Login_Message, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTextField_Login_Username, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                .addComponent(jPasswordField_Login_Password, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jButton_Login_ForgotPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addComponent(jButton_Login_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_Login_MainTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_Login_SubTitle)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_Login_Label)
                    .addComponent(jLabel_Login_Message))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_Login_Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_Login_Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_Login_Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordField_Login_Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_Login_InputVerifyCode, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Login_VerifyCode, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Login_ForgotPassword)
                    .addComponent(jButton_Login_Login))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_Login_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_Login_LoginActionPerformed
        // TODO add your handling code here:
        g_username = jTextField_Login_Username.getText();
        g_password = String.valueOf(jPasswordField_Login_Password.getPassword());
        
        // Test input
        //System.out.println(username);
        //System.out.println(password);
        //System.out.println(UserManagement.MD5("1234545880375"));
        
        if (g_username.length()<6|g_password.length()<4|g_password.length()>15) {
            int b = JOptionPane.showConfirmDialog(this,"Username/Password is wrong","Error",JOptionPane.OK_OPTION);
        } else {
            String temp_InputVerifyCode = jTextField_Login_InputVerifyCode.getText();
        
            if (temp_InputVerifyCode.length()<4) {
                int b = JOptionPane.showConfirmDialog(this,"Please Input Verify Code","Error",JOptionPane.OK_OPTION);
            } else {        

                String userInfo = "Query;Login;"+g_username+";"+g_password;
                
                if (temp_InputVerifyCode.equals(g_verifyCode)) {    
                    MinaClientInterface mci = new MinaClient();
                    mci.createConnect("127.0.0.1",9123);
                    mci.sendMessage(userInfo);
                    
                    String tempRecevieMessage = mci.receiveMessage();
                    System.out.println("Recevied Message From Server: " + tempRecevieMessage);
                     
                    switch(tempRecevieMessage) {
                        case "true":
                            LoginGUI.this.setVisible(false);

                            java.awt.EventQueue.invokeLater(() -> {
                                new Clearence_Delivery(g_username).setVisible(true);
                            });          
                            return;
                        case "online":
                            jLabel_Login_Message.setText("User is Online!!");
                            break;
                        case "false":
                            jLabel_Login_Message.setText("Username/Password is wrong!!");
                            break;
                        default:
                            break;
                    }

                } else {
                    int b = JOptionPane.showConfirmDialog(this,"Verify Code is wrong","Error",JOptionPane.OK_OPTION);
                }   
            }
        }
        
        g_verifyCode = createVerifyCode();
        jLabel_Login_VerifyCode.setIcon(createImageOfVerifyCode(g_verifyCode)); 
        jTextField_Login_InputVerifyCode.setText("");
        jTextField_Login_Username.setText("");
        jPasswordField_Login_Password.setText("");
  
    }//GEN-LAST:event_jButton_Login_LoginActionPerformed

    private void jLabel_Login_VerifyCodeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel_Login_VerifyCodeMouseClicked
        // TODO add your handling code here:
//        VerifyCode v = new VerifyCode();
//        String temp_VerifyCode = v.generateTextCode(v.TYPE_NUM_LOWER,4,null);
//        Icon i = new ImageIcon(v.generateImageCode(temp_VerifyCode,90,30,3,true,null,null,null));
//        jLabel_Login_VerifyCode.setIcon(i);
        g_verifyCode = createVerifyCode();
        jLabel_Login_VerifyCode.setIcon(createImageOfVerifyCode(g_verifyCode));   
        
    }//GEN-LAST:event_jLabel_Login_VerifyCodeMouseClicked

    private String createVerifyCode() {
        VerifyCode v = new VerifyCode();       
        String temp_VerifyCode = v.generateTextCode(v.TYPE_NUM_LOWER,4,null);
        return temp_VerifyCode;
    }
    
    private Icon createImageOfVerifyCode(String temp_VerifyCode) {
        Icon i = new ImageIcon(new VerifyCode().generateImageCode(
                temp_VerifyCode,90,30,3,true,null,null,null));
        return i;      
    }
    
    public boolean checkUsername(String str) {
        System.out.println("CheckUsername:   " + str);
        if ( str.equals("-1")) {
            jLabel_Login_Message.setText("Username/Password is wrong!!");
            System.out.println("Username/Password is wrong!!");
            return false;
        } else {
            jLabel_Login_Message.setText("Welcome!");
            return true;
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Login_ForgotPassword;
    private javax.swing.JButton jButton_Login_Login;
    private javax.swing.JLabel jLabel_Login_Message;
    private javax.swing.JLabel jLabel_Login_VerifyCode;
    private javax.swing.JPasswordField jPasswordField_Login_Password;
    private javax.swing.JTextField jTextField_Login_InputVerifyCode;
    private javax.swing.JTextField jTextField_Login_Username;
    private javax.swing.JLabel label_Login_Label;
    private javax.swing.JLabel label_Login_MainTitle;
    private javax.swing.JLabel label_Login_Password;
    private javax.swing.JLabel label_Login_SubTitle;
    private javax.swing.JLabel label_Login_Username;
    // End of variables declaration//GEN-END:variables
    
    // User Variables declaration
    private String g_username;
    private String g_password;
    private String g_clearance;
    private String g_verifyCode;  
    
}
